package com.huawei.crm.shiro;

import com.alibaba.fastjson.JSON;
import com.huawei.crm.util.JsonResult;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

@Component("crmFormAuthenticationFilter")
public class CRMFormAuthenticationFilter extends FormAuthenticationFilter {
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {

        try {
            response.setContentType("text/json;charset=UTF-8");
            JsonResult result = new JsonResult();
            result.mark("亲,账号或密码错误!");
            response.getWriter().print(JSON.toJSONString(result));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return false;
    }

    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {

        try {
            response.setContentType("text/json;charset=UTF-8");
            JsonResult result = new JsonResult();
            response.getWriter().print(JSON.toJSONString(result));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return false;
    }
}
